import request, { CustomResponse } from "./http_class";
import { IndividualI, SearchParamsI, UploadedFileI } from "../types/system";
import {
  VbpDayStatsI,
  VbpBatchI,
  HisMedicineI,
  MedicineDocI,
  VbpDeptDutyI,
  VbpStatsByCadnI,
  VbpDeptDutyPreviewI,
} from "../types";

export const createIndividual = (
  data: IndividualI
): Promise<CustomResponse<IndividualI>> => {
  return request<IndividualI, IndividualI>({
    url: "/individuals/",
    method: "POST",
    data,
  });
};

export const deleteIndividual = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/individuals/${id}/`,
    method: "DELETE",
  });
};

export const updateIndividual = (
  data: IndividualI
): Promise<CustomResponse<IndividualI>> => {
  return request<IndividualI, IndividualI>({
    url: `/individuals/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const searchIndividualList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: IndividualI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: IndividualI[] }>({
    url: `/individuals/?${paramsString}`,
    method: "GET",
  });
};

export const exportIndividualList = (
  paramsString: string
): Promise<CustomResponse<File>> => {
  return request<SearchParamsI, File>({
    url: `/individuals/export/?${paramsString}`,
    method: "GET",
    responseType: "blob",
  });
};

export const deleteUploadedFile = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/uploaded-files/${id}/`,
    method: "DELETE",
  });
};

export const searchUploadedFileList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: UploadedFileI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: UploadedFileI[] }>({
    url: `/uploaded-files/?${paramsString}`,
    method: "GET",
  });
};

export const getUploadedFile = (id: string): Promise<CustomResponse<any>> => {
  return request<string, any>({
    url: `/uploaded-files/${id}/`,
    method: "GET",
    responseType: "blob",
  });
};

export const generateVbpStatsList = (
  paramsString: string
): Promise<CustomResponse<any>> => {
  return request<any, any>({
    url: `/vbp-stats/generate/?${paramsString}`,
    method: "GET",
  });
};

export const searchVbpStatsList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDayStatsI[] }>> => {
  return request<any, { totalCount: number; data: VbpDayStatsI[] }>({
    url: `/vbp-stats/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpStatsChart = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDayStatsI[] }>> => {
  return request<any, { totalCount: number; data: VbpDayStatsI[] }>({
    url: `/vbp-stats/chart/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpDepartmentList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: any }>> => {
  return request<any, { totalCount: number; data: any }>({
    url: `/vbp-stats/depts/?${paramsString}`,
    method: "GET",
  });
};

//////////////////////////////////////////////////////////////////////////////
export const createVbpBatch = (
  data: VbpBatchI
): Promise<CustomResponse<VbpBatchI>> => {
  return request<VbpBatchI, VbpBatchI>({
    url: "/vbp-batch/",
    method: "POST",
    data,
  });
};

export const deleteVbpBatch = (id: string): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/vbp-batch/${id}/`,
    method: "DELETE",
  });
};

export const updateVbpBatch = (
  data: VbpBatchI
): Promise<CustomResponse<VbpBatchI>> => {
  return request<VbpBatchI, VbpBatchI>({
    url: `/vbp-batch/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getVbpBatchDetail = (
  id: string
): Promise<CustomResponse<VbpBatchI>> => {
  return request<string, VbpBatchI>({
    url: `/vbp-batch/${id}/`,
    method: "GET",
  });
};

export const searchVbpBatchList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpBatchI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpBatchI[] }>({
    url: `/vbp-batch/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpBatchEndDateList = (
  paramsString: string
): Promise<CustomResponse<any>> => {
  return request<SearchParamsI, any>({
    url: `/vbp-batch/end-date/list/?${paramsString}`,
    method: "GET",
  });
};

export const exportVbpBatchList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpBatchI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpBatchI[] }>({
    url: `/vbp-batch/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpStats = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpBatchI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpBatchI[] }>({
    url: `/vbp/stats?${paramsString}`,
    method: "GET",
  });
};

////////////////////////////////////////////////////////////////////////////////////////////////
export const createHisMedicine = (
  data: HisMedicineI
): Promise<CustomResponse<HisMedicineI>> => {
  return request<HisMedicineI, HisMedicineI>({
    url: "/his-medicine/",
    method: "POST",
    data,
  });
};

export const deleteHisMedicine = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/his-medicine/${id}/`,
    method: "DELETE",
  });
};

export const updateHisMedicine = (
  data: HisMedicineI
): Promise<CustomResponse<HisMedicineI>> => {
  return request<HisMedicineI, HisMedicineI>({
    url: `/his-medicine/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getHisMedicineDetail = (
  id: string
): Promise<CustomResponse<HisMedicineI>> => {
  return request<string, HisMedicineI>({
    url: `/his-medicine/${id}/`,
    method: "GET",
  });
};

export const searchHisMedicineList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: HisMedicineI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: HisMedicineI[] }>({
    url: `/his-medicine/?${paramsString}`,
    method: "GET",
  });
};

export const exportHisMedicineList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: HisMedicineI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: HisMedicineI[] }>({
    url: `/his-medicine/?${paramsString}`,
    method: "GET",
  });
};

////////////////////////////////////////////////////////////////////////////////////////////////
export const createMedicineDoc = (
  data: MedicineDocI
): Promise<CustomResponse<MedicineDocI>> => {
  return request<MedicineDocI, MedicineDocI>({
    url: "/medicine-doc/",
    method: "POST",
    data,
  });
};

export const deleteMedicineDoc = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/medicine-doc/${id}/`,
    method: "DELETE",
  });
};

export const updateMedicineDoc = (
  data: MedicineDocI
): Promise<CustomResponse<MedicineDocI>> => {
  return request<MedicineDocI, MedicineDocI>({
    url: `/medicine-doc/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getMedicineDocDetail = (
  id: string
): Promise<CustomResponse<MedicineDocI>> => {
  return request<string, MedicineDocI>({
    url: `/medicine-doc/${id}/`,
    method: "GET",
  });
};

export const searchMedicineDocList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: MedicineDocI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: MedicineDocI[] }>({
    url: `/medicine-doc/?${paramsString}`,
    method: "GET",
  });
};

export const exportMedicineDocList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: MedicineDocI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: MedicineDocI[] }>({
    url: `/medicine-doc/?${paramsString}`,
    method: "GET",
  });
};

//////////////////////////////////////////////////////////////////////////////
export const generateVbpDeptDutyPreview = (): Promise<CustomResponse<any>> => {
  return request<any, any>({
    url: "/vbp-dept-duty-pre/generate/",
    method: "POST",
  });
};

export const batchUseVbpDeptDutyPreview = (
  data: VbpDeptDutyPreviewI[]
): Promise<CustomResponse<any>> => {
  return request<VbpDeptDutyPreviewI[], any>({
    url: "/vbp-dept-duty-pre/batch-use/",
    method: "POST",
    data: data,
  });
};

export const searchVbpDeptDutyPreviewList = (
  paramsString: string
): Promise<
  CustomResponse<{ totalCount: number; data: VbpDeptDutyPreviewI[] }>
> => {
  return request<
    SearchParamsI,
    { totalCount: number; data: VbpDeptDutyPreviewI[] }
  >({
    url: `/vbp-dept-duty-pre/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpDeptDutyPreviewDetail = (
  id: string
): Promise<CustomResponse<VbpDeptDutyPreviewI>> => {
  return request<string, VbpDeptDutyPreviewI>({
    url: `/vbp-dept-duty-pre/${id}/`,
    method: "GET",
  });
};

export const deleteVbpDeptDutyPreview = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/vbp-dept-duty-pre/${id}/`,
    method: "DELETE",
  });
};

export const updateVbpDeptDutyPreview = (
  data: Partial<VbpDeptDutyPreviewI>
): Promise<CustomResponse<VbpDeptDutyPreviewI>> => {
  return request<Partial<VbpDeptDutyPreviewI>, VbpDeptDutyPreviewI>({
    url: `/vbp-dept-duty-pre/${data.id}/`,
    method: "PUT",
    data,
  });
};

//////////////////////////////////////////////////////////////////////////////
export const createVbpDeptDuty = (
  data: VbpDeptDutyI
): Promise<CustomResponse<VbpDeptDutyI>> => {
  return request<VbpDeptDutyI, VbpDeptDutyI>({
    url: "/vbp-dept-duty/",
    method: "POST",
    data,
  });
};

export const deleteVbpDeptDuty = (
  id: string
): Promise<CustomResponse<string>> => {
  return request<string, string>({
    url: `/vbp-dept-duty/${id}/`,
    method: "DELETE",
  });
};

export const updateVbpDeptDuty = (
  data: VbpDeptDutyI
): Promise<CustomResponse<VbpDeptDutyI>> => {
  return request<VbpDeptDutyI, VbpDeptDutyI>({
    url: `/vbp-dept-duty/${data.id}/`,
    method: "PUT",
    data,
  });
};

export const getVbpDeptDutyDetail = (
  id: string
): Promise<CustomResponse<VbpDeptDutyI>> => {
  return request<string, VbpDeptDutyI>({
    url: `/vbp-dept-duty/${id}/`,
    method: "GET",
  });
};

export const searchVbpDeptDutyList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDeptDutyI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpDeptDutyI[] }>({
    url: `/vbp-dept-duty/?${paramsString}`,
    method: "GET",
  });
};

export const searchVbpDeptDutyStats = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDeptDutyI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpDeptDutyI[] }>({
    url: `/vbp-dept-duty/sql/?${paramsString}`,
    method: "GET",
  });
};

export const exportVbpDeptDutyStats = (
  paramsString: string
): Promise<CustomResponse<any>> => {
  return request<SearchParamsI, any>({
    url: `/vbp-dept-duty/export/?${paramsString}`,
    method: "POST",
    responseType: "blob", // 不写responseType也能下载文件，但会导致文件破坏打不开
  });
};

export const exportVbpDeptDutyList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDeptDutyI[] }>> => {
  return request<SearchParamsI, { totalCount: number; data: VbpDeptDutyI[] }>({
    url: `/vbp-dept-duty/?${paramsString}`,
    method: "GET",
  });
};

//////////////////////////////////////////////////////////////////////////////

export const getCasbinConfig = (): Promise<CustomResponse<string>> => {
  return request<any, string>({
    url: `/casbin-rule/config/`,
    method: "GET",
  });
};

export const getCasbinPolicies = (): Promise<CustomResponse<any>> => {
  return request<any, any>({
    url: `/casbin-rule/policies/`,
    method: "GET",
  });
};

////////////////////////////////////////////////////////////////////////////////////////////////
// 中选药品全院完成情况
export const getVbpStatsByHisCode = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDayStatsI[] }>> => {
  return request<any, { totalCount: number; data: VbpDayStatsI[] }>({
    url: `/vbp-stats/by/his-code/?${paramsString}`,
    method: "GET",
  });
};

export const getVbpStatsByHisCodeDept = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpDayStatsI[] }>> => {
  return request<any, { totalCount: number; data: VbpDayStatsI[] }>({
    url: `/vbp-stats/by/his-code/dept/?${paramsString}`,
    method: "GET",
  });
};

// 集采相关药品全院完成情况
export const getVbpStatsByCadn = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpStatsByCadnI[] }>> => {
  return request<any, { totalCount: number; data: VbpStatsByCadnI[] }>({
    url: `/vbp-stats/by/cadn/?${paramsString}`,
    method: "GET",
  });
};

// 某通用名和集采别的药品各科室使用情况
export const getVbpStatsByCadnVbp = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: VbpStatsByCadnI[] }>> => {
  return request<any, { totalCount: number; data: VbpStatsByCadnI[] }>({
    url: `/vbp-stats/by/cadn/vbp/?${paramsString}`,
    method: "GET",
  });
};

// 按科室统计各科集采相关药品完成情况
export const getVbpStatsByCadnDept = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: any }>> => {
  return request<any, { totalCount: number; data: any }>({
    url: `/vbp-stats/by/cadn/dept/?${paramsString}`,
    method: "GET",
  });
};

// 按日期统计各药品使用情况
export const getVbpStatsByStatsDate = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: any }>> => {
  return request<any, { totalCount: number; data: any }>({
    url: `/vbp-stats/by/stats-date/?${paramsString}`,
    method: "GET",
  });
};

// 集采相关访问日志
export const searchAccessLogList = (
  paramsString: string
): Promise<CustomResponse<{ totalCount: number; data: any }>> => {
  return request<any, { totalCount: number; data: any }>({
    url: `/access-logs/sql/?${paramsString}`,
    method: "GET",
  });
};

////////////////////////////////////////////////////////////////////////////////////////////////
// 药学咨询
export const getChatResponse = (data: any): Promise<CustomResponse<string>> => {
  return request<any, string>({
    url: "/chat_basic/generate/",
    method: "POST",
    data,
  });
};

// 提交问题
export const postChatQuery = (data: any): Promise<CustomResponse<string>> => {
  return request<any, string>({
    // url: "/chat_transformer/chat/",
    // url: "/chat_vllm/chat/",
    url: "/chat_vllm/stream/",
    method: "POST",
    data,
  });
};

// 获取流式结果
export const getChatStream = (): Promise<CustomResponse<string>> => {
  return request<any, string>({
    // url: "/chat_transformer/stream/",
    url: "/chat_vllm/stream/",
    method: "GET",
  });
};

// 提交问题
export const postChatVllmQuery = (
  data: any
): Promise<CustomResponse<string>> => {
  return request<any, string>({
    url: "/chat_vllm/chat/",
    method: "POST",
    data,
  });
};

// 获取流式结果
export const getChatVllmStream = (): Promise<CustomResponse<string>> => {
  return request<any, string>({
    url: "/chat_vllm/stream/",
    method: "GET",
  });
};
